package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;


public class CaiPuCommentRowMapper implements RowMapper<CaiPuComment>{

    private int type;

    public static final int AND_USERNAME = 1;
    
    public CaiPuCommentRowMapper() {
    }

    public CaiPuCommentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public CaiPuComment mapRow(ResultSet rs, int rowNum) throws SQLException {
        CaiPuComment cp = new CaiPuComment();
        switch (type) {
            case AND_USERNAME:
                cp = parseCaiPuComment(new CaiPuComment(), rs);
                cp.setRealname(rs.getString("realname"));
                break;
            default:
                cp = parseCaiPuComment(new CaiPuComment(), rs);
        }
        return cp;
    }

    public static CaiPuComment parseCaiPuComment(CaiPuComment cp, ResultSet rs)
        throws SQLException {
        return parseCaiPuComment(cp, rs, null);
    }

    public static CaiPuComment parseCaiPuComment(CaiPuComment cp, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            cp.setCcid(rs.getLong(su + "ccid"));
        } catch (SQLException e) {
            cp.setCcid(null);
        }
        try {
            cp.setCp_id(rs.getLong(su + "cp_id"));
        } catch (SQLException e) {
            cp.setCp_id(null);
        }
        try {
            cp.setUid(rs.getLong(su + "uid"));
        } catch (SQLException e) {
            cp.setUid(null);
        }
        try {
            cp.setComment(rs.getString(su + "comment"));
        } catch (SQLException e) {
            cp.setComment(null);
        }
        try {
            cp.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            cp.setCdate(null);
        }  
        return cp;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            CaiPuComment cp) {
    	par.put("ccid", cp.getCcid());
        par.put("cp_id", cp.getCp_id());
        par.put("uid", cp.getUid());
        par.put("comment", cp.getComment());
        par.put("cdate", cp.getCdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            CaiPuComment cp) {
        args.put("ccid", cp.getCcid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final CaiPuComment CP) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(CP),
                        new String[] { "ccid" });
                parsePS(ps, CP);
                return ps;
            }

        };
    }

    private static String parseSQL(CaiPuComment cp) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_caipu_comment(");
        int count = 0;
        if (null != cp.getCcid()) {
            sql.append("ccid,");
            count++;
        }
        if (null != cp.getCp_id()) {
            sql.append("cp_id,");
            count++;
        }
        if (null != cp.getUid()) {
            sql.append("uid,");
            count++;
        }
        if (null != cp.getComment()) {
            sql.append("comment,");
            count++;
        }
        if (null != cp.getCdate()) {
            sql.append("cdate,");
            count++;
        }
      
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, CaiPuComment cp)
        throws SQLException {
        int count = 1;
        if (null != cp.getCcid()) {
            ps.setLong(count++, cp.getCcid());
        }        
        if (null != cp.getCp_id()) {
            ps.setLong(count++, cp.getCp_id());
        }
        if (null != cp.getUid()) {
            ps.setLong(count++, cp.getUid());
        }
        if (null != cp.getComment()) {
            ps.setString(count++, cp.getComment());
        }        
        if (null != cp.getCdate()) {
            ps.setTimestamp(count++, cp.getCdate());
        } 

    }

}
